package net.maergaiyun.controller;


import lombok.extern.slf4j.Slf4j;
import net.maergaiyun.annotation.RepeatSubmit;
import net.maergaiyun.constant.RedisKey;
import net.maergaiyun.controller.request.ConfirmOrderRequest;
import net.maergaiyun.controller.request.ProductOrderPageRequest;
import net.maergaiyun.enums.BizCodeEnum;
import net.maergaiyun.enums.ClientTypeEnum;
import net.maergaiyun.enums.ProductOrderPayTypeEnum;
import net.maergaiyun.interceptor.LoginInterceptor;
import net.maergaiyun.service.ProductOrderService;
import net.maergaiyun.util.CommonUtil;
import net.maergaiyun.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author maergaiyun
 * @since 2024-09-05
 */
@RestController
@RequestMapping("/api/order/v1")
@Slf4j
public class ProductOrderController {
    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("page")
    public JsonData page(@RequestBody ProductOrderPageRequest request) {
        Map<String, Object> pageResult = productOrderService.page(request);

        return JsonData.buildSuccess(pageResult);
    }


    @GetMapping("query_state")
    public JsonData queryState(@RequestParam("out_trade_no") String outTradeNo) {
        String state = productOrderService.queryProductOrderState(outTradeNo);
        return StringUtils.isBlank(state) ? JsonData.buildResult(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST) : JsonData.buildSuccess(state);
    }

    @PostMapping("confirm")
//    @RepeatSubmit(limitType = RepeatSubmit.Type.PARAM)
    public void confirmOrder(@RequestBody ConfirmOrderRequest request, HttpServletResponse response) {
        JsonData jsonData = productOrderService.confirmOrder(request);
        if (jsonData.getCode() == 0) {
            String client = request.getClientType();
            String payType = request.getPayType();

            if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.ALI_PAY.name())) {
                if (client.equalsIgnoreCase(ClientTypeEnum.PC.name())) {
                    CommonUtil.sendHtmlMessage(response, jsonData);

                } else if (client.equalsIgnoreCase(ClientTypeEnum.APP.name())) {

                } else if (client.equalsIgnoreCase(ClientTypeEnum.H5.name())) {

                }
            } else if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.WECHAT_PAY.name())) {
                CommonUtil.sendJsonMessage(response, jsonData);
            }

        } else {
            log.error("创建订单失败: {}", jsonData);
            CommonUtil.sendJsonMessage(response, jsonData);
        }
    }


    @GetMapping("token")
    public JsonData getOrderToken() {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        String token = CommonUtil.getStringNumRandom(32);
        String key = String.format(RedisKey.SUBMIT_ORDER_TOKEN_KEY, accountNo, token);

        redisTemplate.opsForValue().set(key, String.valueOf(Thread.currentThread().getId()), 30, TimeUnit.MINUTES);
        return JsonData.buildSuccess(token);
    }
}

